#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>

int main(void)
{
	pid_t pid;
	void *ptr;

	// Memory map必须要有亲缘关系的内存才可以使用
	ptr = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
	/*
	int shmid = shmget(IPC_PRIVATE, 1024, IPC_CREAT | IPC_EXCL | 0600);	
	if (-1 == shmid) {
		perror("Error getting shared memory");
		exit(1);
	}
	*/
	pid = fork();
	if (-1 == pid) {
		perror("Error forking process");
		exit(1);
	}
	if (0 == pid) {
		//ptr = shmat(shmid, NULL, 0);
		printf("Child: %p\n", ptr);

		memcpy(ptr, "Hello, World!", 14);
		munmap(ptr, 1024);
		//shmdt(ptr);
		exit(0);
	}

	wait(NULL);
	//ptr = shmat(shmid, NULL, 0);
	printf("Parent: %p\n", ptr);

	puts(ptr);
	munmap(ptr, 1024);
	//shmdt(ptr);

	//shmctl(shmid, IPC_RMID, NULL);

	exit(0);
}
